<template>
  <div class="addgiftbox">
    <el-form ref="giftboxForm" :model="giftboxForm" class="giftbox_form" auto-complete="on" :rules="giftboxRules">
      <el-form-item prop="giftboxName" label="鲜花名称:">
        <el-input v-model="giftboxForm.giftboxName" placeholder="请输入名称"></el-input>
      </el-form-item>

      <el-form-item prop="giftboxPrice" label="鲜花价格">
        <el-input v-model="giftboxForm.giftboxPrice" placeholder="请输入价格"></el-input>
      </el-form-item>
      <el-form-item prop="giftboxPrice" label="鲜花库存">
        <el-input v-model="giftboxForm.giftboxStore" placeholder="请输入库存"></el-input>
      </el-form-item>
      <el-form-item prop="giftboxPrice" label="开启售卖">
        <el-radio v-model="radio" label="1">是</el-radio>
        <el-radio v-model="radio" label="2">否</el-radio>
      </el-form-item>
      <el-form-item prop="giftboxImg" label="鲜花图片">
        <!-- <el-upload action="#" list-type="picture-card" :auto-upload="false" multiple=false>
          <i slot="default" class="el-icon-plus"></i>
          <div slot="file" slot-scope="{ file }">
            <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
            <span class="el-upload-list__item-actions">
              <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                <i class="el-icon-zoom-in"></i>
              </span>
              <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
                <i class="el-icon-download"></i>
              </span>
              <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
        </el-upload> -->

        <el-upload class="avatar-uploader" action="http://localhost:9600/admin/common/upload" :show-file-list="false"
          :on-success="handleAvatarSuccess" :limit="1">
          <img v-if="this.giftboxForm.giftboxImg" :src="this.giftboxForm.giftboxImg" class="avatar" >
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>




        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>

      <el-form-item prop="giftboxDescription" label="鲜花描述">
        <el-input type="textarea" v-model="giftboxForm.giftboxDescription" placeholder="鲜花描述,最长200字"></el-input>
      </el-form-item>


    </el-form>
    <div class="myButton">
      <el-button type="warning" round @click="goback()">返回</el-button>
      <el-button type="warning" round @click="savegiftboxClick()">保存</el-button>
    </div>
  </div>
</template>

<script>
import {savegiftbox,editgiftbox} from '@/api/giftbox'
export default {
  data() {
    return {
      imageUrl:'',
      radio: '1',
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      giftboxForm: {},
      giftboxRules: {
        giftboxName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        giftboxPrice: [
          { required: true, message: "价格不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {

        this.giftboxForm.giftboxImg = res.data
        // this.giftboxForm.giftboxImg = URL.createObjectURL(file.raw);


      },
      /* beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;

    }, */
    savegiftboxClick() {

      savegiftbox(this.giftboxForm).then((res)=>{
        this.$message({
          type: 'success',
          message: '编辑成功!'
        });
      })
      //
      this.$router.go(-1);
    },
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    goback() {
      this.$router.go(-1)
    }
  },
};
</script>

<style lang="scss">
.addgiftbox {
  // .main{
  //   width: 80%;
  //   margin: auto;
  //   border: 1px solid black;
  // }
  // border: 1px solid black;
  width: 90%;
  margin: auto;
  margin-top: 30px;

  .el-form-item {
    // 重置或调整 el-form-item 的样式
    margin-bottom: 20px; // 根据需要调整

    // 调整 label 的宽度
    .el-form-item__label {
      width: 100px; // 根据您的需要调整
      text-align: right; // 使标签右对齐
    }

    // 调整 content 的 margin 以与 label 对齐
    .el-form-item__content {
      margin-left: 120px; // 通常等于 label 的宽度加上一些间距
    }

    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }


  }

  .giftbox_form {
    .el-input__inner {
      // margin-left: 50px;
      width: 70%;
    }
  }

  .myButton {
    margin-left: 500px;
  }
}
</style>
